package com;

import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.PageFactory;

import pages.FillMoviePage;
import pages.InternalPage;
import pages.LoginPage;
import pages.MainPage;
import pages.MoviePage;
import pages.AnyPage;

public class AddAndDeleteNewMovie extends TestBase {

	private LoginPage loginPage;
	private FillMoviePage fillMoviePage;
	private InternalPage internalPage;
	private MainPage mainPage;
	private MoviePage moviePage;
	private AnyPage page;
	private String testMovieName;

	@Test
	public void main() throws Exception {
		goToPage("http://localhost/php4dvd/");
		checkForSqlErrorMassage();
		loginTest("admin", "admin");
		assertTrue("Кнопка Add Movie, не найдена", isAddMovieButtonExist());
		goToFillMovieFormPage();
		checkForSqlErrorMassage();
		testMovieName = testMovieNameGen("Test Movie ");
		feelTestMovieForm();
		assertTrue("saveMovieFormButton не найдена", isSaveMovieButtonExist());
		saveMovieClick();
		isOnMoviePage();
		assertTrue(isTestMovieAddedOnMoviePage());
		saveMovieLink();
		// String saveMovieLinkString = saveMovieLink();
		goToMainPage();
		wait(100);
		assertTrue(isTestMovieAddedOnMainPage());
		goToCurrentMoviePageByXpath(testMovieName);
		// goToPage(saveMovieLinkString);
		assertTrue(isTestMovieAddedOnMoviePage());
		removeTestMovie();
		checkForSqlErrorMassage();
	}

	private void goToCurrentMoviePageByXpath(String testMovieName) {
		mainPage.idResultXpath.findElement(
				By.xpath("//a[contains(.,'" + testMovieName + "')]")).click();
	}

	private void removeTestMovie() {
		moviePage.removeButton.click();
		driver.switchTo().alert().accept();

	}

	private void goToMainPage() {
		internalPage.logo.click();
	}

	private String saveMovieLink() {
		String testMovieUrl = driver.getCurrentUrl();
		return testMovieUrl;
	}

	private boolean isOnMoviePage() {
		return moviePage.removeButton.isDisplayed();

	}

	private boolean isTestMovieAddedOnMoviePage() {
		return page.htmlBodyXpath.getText().contains(testMovieName);
	}

	private String testMovieNameGen(String f_testMovieName) {
		int i = (int) Math.floor(Math.random() * 1000000);
		String aString = Integer.toString(i);
		return f_testMovieName.concat(aString);
	}

	private boolean isTestMovieAddedOnMainPage() {
		String i = mainPage.idResultXpath.getText();
		return i.contains(testMovieName);
	}

	private boolean isAddMovieButtonExist() {
		return driver.findElements(By.cssSelector("img[alt=\"Add movie\"]"))
				.size() > 0;
	}

	private void saveMovieClick() {
		fillMoviePage.saveMovieFormButton.click();
	}

	private boolean isSaveMovieButtonExist() {
		return fillMoviePage.saveMovieFormButton.isDisplayed();
	}

	private boolean checkForSqlErrorMassage() {
		String i = page.htmlBodyXpath.getText();
		boolean f = i.contains("Fatal error") && i.contains("SQLSTATE")
				&& i.contains("Uncaught") && i.contains("Notice");
		return f;
	}

	private void goToFillMovieFormPage() {
		mainPage.addMovieButton.click();

	}

	private void feelTestMovieForm() {

		fillMoviePage.imdbNumber.clear();
		fillMoviePage.imdbNumber.sendKeys("1");
		fillMoviePage.imdbNumber.clear();
		fillMoviePage.imdbNumber.sendKeys("2");
		fillMoviePage.MovieName.clear();
		fillMoviePage.MovieName.sendKeys(testMovieName);
		fillMoviePage.AkaMovieName.clear();
		fillMoviePage.AkaMovieName.sendKeys(testMovieName);
		fillMoviePage.MovieYear.clear();
		fillMoviePage.MovieYear.sendKeys("1980");
		fillMoviePage.MovieDuration.clear();
		fillMoviePage.MovieDuration.sendKeys("120");
		fillMoviePage.MovieduRating.clear();
		fillMoviePage.MovieduRating.sendKeys("10");
		fillMoviePage.rbIownThisMovie.click();
		fillMoviePage.rbSeenNo.click();
		fillMoviePage.rbLoanedYes.click();
		fillMoviePage.MoviePersonalNotes.clear();
		fillMoviePage.MoviePersonalNotes.sendKeys("Personal notes");
		fillMoviePage.MovieTaglines.clear();
		fillMoviePage.MovieTaglines.sendKeys("Taglines 1");

	}

	private void loginTest(String userName, String password) {
		loginPage.userNameFieldOnLoginPage.clear();
		loginPage.userNameFieldOnLoginPage.sendKeys(userName);
		loginPage.passwordField.clear();
		loginPage.passwordField.sendKeys(password);
		loginPage.loginButton.click();
	}

	private void goToPage(String url) {
		driver.get(url);
		
		loginPage = PageFactory.initElements(driver, LoginPage.class);
		fillMoviePage = PageFactory.initElements(driver, FillMoviePage.class);
		internalPage = PageFactory.initElements(driver, InternalPage.class);
		page = PageFactory.initElements(driver, AnyPage.class);
		mainPage = PageFactory.initElements(driver, MainPage.class);
		moviePage = PageFactory.initElements(driver, MoviePage.class);

	}

}
